package com.bert.thymeleafgenerator.controller;

import com.bert.thymeleafgenerator.config.ThymeLeafConfig;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class ThymeleafController {

    @GetMapping("/generator")
    @ResponseBody
    public Boolean generator(){
        String stored = "call getinfo(varchar bm, varchar qzzd, varchar tjzd, varchar tj)";
        Pattern pattern = Pattern.compile("\\(([\\s\\S])*?\\)");
        Matcher matcher = pattern.matcher(stored);
        String s = new String();
        while (matcher.find()){
            System.out.println(matcher.group());
            s = matcher.group().substring(1, matcher.group().length() -1);
        }
        String[] t = s.split(",");
        List list = new ArrayList();
        List list_Param = new ArrayList();
        for (int i = 0; i < t.length; i++) {

            String[] temp = t[i].split("\\s+");
//            (i < (t.length - 1)) ? list.add("String " + temp[1]) : list.add("String " + temp[1] + ", ")
            list.add("String " + temp[1]);
        }
        for (int i = 0; i < t.length; i++) {
            String[] temp = t[i].split("\\s+");
            list_Param.add("" + temp[1]);
        }

        Context context = new Context();
        context.setVariable("storedProcedure","gen_bcj_cjld_cf(cjldid  int,jl int,ld_sj datetime)");
        context.setVariable("className","test1");
        context.setVariable("methodName","testByID1");
        context.setVariable("remark","这是一个测试消息1");
        context.setVariable("list",list);
        return doGenerator(context, "mapper.java","Mapper.java");
    }



    @GetMapping("/generator/mapper")
    @ResponseBody
    public Boolean mapper(){
        String stored = "gen_bcj_cjld_cf(cjldid  int,jl int,ld_sj datetime)";
        Pattern pattern = Pattern.compile("(\\([^\\)]*\\))");
        Matcher matcher = pattern.matcher(stored);
        while (matcher.find()){
            System.out.println(matcher.group());
        }
        Context context = new Context();
        context.setVariable("storedProcedure","gen_bcj_cjld_cf(cjldid  int,jl int,ld_sj datetime)");
        context.setVariable("className","test1");
        context.setVariable("methodName","testByID1");
        context.setVariable("remark","这是一个测试消息1");
        return doGenerator(context, "mapper.java","Mapper.java");
    }

    @GetMapping("/generator/controller")
    @ResponseBody
    public Boolean controller(){
        Context context = new Context();
        context.setVariable("routing","test");
        context.setVariable("restful","PutMapping");
        context.setVariable("className","test2");
        context.setVariable("methodName","testByID2");
        context.setVariable("remark","这是一个测试消息2");
        return doGenerator(context, "Controller.java","Controller.java");
    }

    @GetMapping("/testIF")
    @ResponseBody
    public Boolean testIF(){
        Context context = new Context();
        //添加基本类型
        context.setVariable("name","yangzhenyu");
        context.setVariable("flag",true);
        return doGenerator(context, "Test_If.123","TEST_IF.java");
    }

    @GetMapping("/testFOR")
    @ResponseBody
    public Boolean testFOR(){
        Context context = new Context();
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("fieldName","name");
        map.put("remarks","姓名");
        Map<String,String> map1 = new HashMap<>();
        map1.put("fieldName","age");
        map1.put("remarks","年龄");
        list.add(map);
        list.add(map1);
        context.setVariable("lists",list);
        return doGenerator(context, "Test_For.123","TEST_FOR.java");
    }

    /**
     * 生成文件
     *
     * @param context 参数内容
     * @param fileName 生成文件的绝对路径
     * @param templateName 使用模板名
     * @return 执行状态
     */
    public Boolean doGenerator(Context context, String fileName, String templateName){
        String out = ThymeLeafConfig.getTemplateString(templateName, context);
        System.out.println(out);
        //生成的文件根路径
        //String root = System.getProperty("user.dir") + "\\src\\main\\java\\com\\bert\\thymeleafgenerator\\generator\\";
        String filePath = "src/main/resources/Code_Auto_File/" + fileName;
        //生成文件在根目录下
        //String filePath = "generatedFile/" + fileName;
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try {
            Files.writeString(Paths.get(filePath), out);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
